<?php

require_once ('system\core\Model.php');

class newsinfoModel extends CI_Model {
	
	function __construct() {
		parent::__construct ();
		$this->load->library('session'); 
		$this->load->database();
	}
	function addnewsinfo($case_name,$recommend,$pic,$case_content)
	{
	   $filename="";
	   $recommend=$recommend ? $recommend :0;
		if($pic['size']>0 && $pic['name']!=""){
            $date1=date("YmdHis");
			$ext=explode(".",$pic['name']);
			$filename="template/upfile/".$date1.".".$ext[1];
			if (!move_uploaded_file($pic['tmp_name'],$filename)) {
			
					echo "<script>alert('复制文件失败');history.back()</script>";

					exit;
			}
		}
		$sql = "insert into news values(?,?,?,?,?,?,?,?)"; 
		$this->db->query($sql, array(null,$case_name,$recommend,$filename,$case_content,0,0,time())); 		
		return $this->db->affected_rows();
				
	}
	function updatenewsinfo($id,$new_name,$recommend,$pic,$new_content)
	{
	     $filename="";
	   $recommend=$recommend ? $recommend :0;
		if($pic['size']>0 && $pic['name']!=""){
            $date1=date("YmdHis");
			$ext=explode(".",$pic['name']);
			$filename="template/upfile/".$date1.".".$ext[1];
			if (!move_uploaded_file($pic['tmp_name'],$filename)) {
			
					echo "<script>alert('复制文件失败');history.back()</script>";

					exit;
			}
		}
		if($filename){
		$sql = "update news set new_name=?,recommend=?,pic=?,new_content=? where id=?";
		$arr= array($new_name,$recommend,$filename,$new_content,$id);
		}else{
		 $sql = "update news set new_name=?,recommend=?,new_content=? where id=?";
		$arr= array($new_name,$recommend,$new_content,$id);
		}
		$this->db->query($sql,$arr); 
		return $this->db->affected_rows();
				
	}
	function getnewsinfodetail($id)
	{
		$sql="select * from news where id=?";
		$query = $this->db->query($sql, array($id));
		return $query->result_array();
	}
	function delete($id)
	{
		$sql="delete from news where id=?";
		$query = $this->db->query($sql, array($id));
		return $this->db->affected_rows();
	}
	function getnewsinfo()
	{
		return $this->db->count_all('news');  
	}
	function getnewslist($start,$size)
	{
		$sql="select * from news limit ?,?";
		$query = $this->db->query($sql, array($start,$size));
		return $query;
	}
	function gettopnewslist($size)
	{
		$sql="select * from news where recommend=1 limit $size";
		$query = $this->db->query($sql, array($size));
		return $query;
	}
	
	function getcaseinfoByCond($case_name
	,$case_type
	,$case_status
	,$incident_time_begin
	,$incident_time_end
	,$court_date_begin
	,$court_date_end
	,$prosecutor
	,$defendant)
	{
		
		$sql="select count(*) as cnt from caseinfo where case_status = ? "
		 ." and case_type=? and case_name like ? "
		 ." and defendant like ? and prosecutor like ? "
		 ." and (court_date >= ? or '' = ? ) "
		 ." and (court_date <= ? or '' = ? ) "
		 ." and (incident_time >= ? or '' = ? ) "
		 ." and (incident_time <= ? or '' = ? ) ";
		$query = $this->db->query($sql, array($case_status
			,$case_type
			,"%".$case_name."%"
			,"%".$defendant."%"
			,"%".$prosecutor."%"
			,$court_date_begin
			,$court_date_begin
			,$court_date_end
			,$court_date_end
			,$incident_time_begin
			,$incident_time_begin
			,$incident_time_end
			,$incident_time_end));
		 $array= $query->result_array();
		return $array[0]["cnt"];
	}
	function getcaselistByCond($start,$size,$case_name
		,$case_type
		,$case_status
		,$incident_time_begin
		,$incident_time_end
		,$court_date_begin
		,$court_date_end
		,$prosecutor
		,$defendant)
	{
		$sql="select * from caseinfo where case_status = ? "
		 ." and case_type=? and case_name like ? "
		 ." and defendant like ? and prosecutor like ? "
		 ." and (court_date >= ? or '' = ? ) "
		 ." and (court_date <= ? or '' = ? ) "
		 ." and (incident_time >= ? or '' = ? ) "
		 ." and (incident_time <= ? or '' = ? ) "
		 ." limit ?,?";
		$query = $this->db->query($sql, array($case_status
			,$case_type
			,"%".$case_name."%"
			,"%".$defendant."%"
			,"%".$prosecutor."%"
			,$court_date_begin
			,$court_date_begin
			,$court_date_end
			,$court_date_end
			,$incident_time_begin
			,$incident_time_begin
			,$incident_time_end
			,$incident_time_end
			,$start
			,$size
			));
		return $query;
		
	}
}

?>